package com.sinoeyes.controller.system;


import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.core.page.RestPageResult;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.filter.SysDictDataFilter;
import com.sinoeyes.system.service.ISysDictDataService;
import com.sinoeyes.system.vo.SysDictDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据字典信息
 *
 */
@RestController
@RequestMapping("/system/dict/data")
@Api(value = "数据字典信息 API", tags = {"数据字典信息"})
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("查询字典分页数据")
    public Result<RestPageResult<SysDictDataVo>> list(SysDictDataFilter dictDataFilter)
    {
        RestPageResult<SysDictDataVo> pageResult = dictDataService.selectDictDataList(dictDataFilter);
        return Result.success(pageResult);
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    @ApiOperation("查询字典数据详细")
    public Result<SysDictDataVo> getInfo(@PathVariable Long dictCode)
    {
        return Result.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/dictType/{dictType}")
    @ApiOperation("根据字典类型查询字典数据信息")
    public Result<List<SysDictDataVo>> dictType(@PathVariable String dictType)
    {
        return Result.success(dictDataService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增字典类型")
    public Result<Integer> add(@RequestBody SysDictDataVo dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return Result.success(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改保存字典类型")
    public Result<Integer> edit(@RequestBody SysDictDataVo dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCode}")
    @ApiOperation("删除字典类型")
    public Result<Integer> remove(@PathVariable Long dictCode)
    {
        return Result.success(dictDataService.deleteDictDataById(dictCode));
    }
}
